package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.pojo.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        //获取登录人的id
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
        //获取dishid
        Long dishId = shoppingCart.getDishId();

        //判断是否存在dishid值 如果有添加到购物车中的是菜品
        //否说明添加的为套餐
        if (dishId != null) {
            //添加的四菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加的是套餐
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //根据用用户id 和菜品id 判断此数据是否存在
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
        //说明数据库中没有添加
        if (one == null) {
            //没有就需要新增
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(currentId);
            shoppingCartService.save(shoppingCart);
            //给one 赋值
            one= shoppingCart;
        } else {
            //如果有数据 将numb+1
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);

        }
        //ShoppingCart shoppingCart1 = shoppingCartService.getOne(lambdaQueryWrapper);
        //不能直接返回one,one可能没有数据,所以需要给one赋值
        return R.success(one);
    }

    @GetMapping("/list")
    public R list() {
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 取消购物车
     * @return
     */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        //说明还有数据 更新numb值
        if (number>0){
            shoppingCartService.updateById(one);
        }
        //没有值 清空购物车
        if (number<=0){
           shoppingCartService.remove(lambdaQueryWrapper);
        }
        return R.success(one);


    }
    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> deleteById(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清空购物车成功");
    }


}
